package cn.jy.finanlle.controller;


import cn.jy.common.domain.financea.HuoBi;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/huobi")
@CrossOrigin
public class CBTCurrenciesFetcher {
    @Autowired
    private RedisTemplate redisTemplate;

//    @PostMapping("/huobiselect")
//    public List<String> huobiselect() {
//        String url = "https://www.boc.cn/sourcedb/whpj/index.html"; // 中国银行交易币列表页面
//        List<String> currencies = getCurrencies(url);
////        System.out.println("1111111"+currencies);
////        for (String currency : currencies) {
////            System.out.println(currency);
////        }
//        return currencies;
//    }

        @GetMapping("/huobiselect")
    public List<HuoBi> huobiselect(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        String url = "https://www.boc.cn/sourcedb/whpj/index.html"; // 中国银行交易币列表页面
            List<HuoBi> huobi = redisTemplate.opsForList().range("huobi", 0, -1);
            if (huobi != null && !huobi.isEmpty()){
                return redisTemplate.opsForList().range("huobi", pageNum, pageSize);
            }else {
                List<HuoBi> currencies = getCurrencies(url);
                redisTemplate.opsForList().rightPushAll("huobi", currencies);
                redisTemplate.expire("huobi",2,TimeUnit.HOURS);
                return currencies;
            }
//            List<HuoBi> currencies = getCurrencies(url);
//        System.out.println("1111111"+currencies);
//        for (String currency : currencies) {
//            System.out.println(currency);
//        }
//        return currencies;
    }

//    public static void main(String[] args) {
//        String url = "https://www.boc.cn/sourcedb/whpj/index.html"; // 中国银行交易币列表页面
//        List<String> currencies = getCurrencies(url);
//        System.out.println("1111111"+currencies);
//        for (String currency : currencies) {
//            System.out.println(currency);
//        }
//    }

    private static List<HuoBi> getCurrencies(String url) {
        List<String> currencies = new ArrayList<>();
        ArrayList<HuoBi> huobis = new ArrayList<>();
        try {
            Document doc = Jsoup.connect(url).get();
            System.out.println(doc);

            Element table = doc.select("table").get(1);
            System.out.println("77777777777777777"+table);
            if (table != null) { // 检查是否找到了table元素

                Elements rows = table.select("tr");
//                for (Element row : rows) {
//                    Elements tds = row.select("td");
//                    String currency = tds.text();
//
//                    currencies.add(currency);
//                }
                for (int i = 1; i < rows.size(); i++) { // 第一行是表头，所以从第二行开始
                    Elements tds = rows.get(i).select("td");
                    HuoBi huoBi = new HuoBi();
                    String currency0 = tds.get(0).text();
                    huoBi.setName(currency0);
                    String currency1 = tds.get(1).text();
                    huoBi.setMrj(currency1);
                    String currency2 = tds.get(2).text();
                    huoBi.setXc(currency2);
                    String currency3 = tds.get(3).text();
                    huoBi.setXh(currency3);
                    String currency4 = tds.get(4).text();
                    huoBi.setXcmc(currency4);
                    String currency5 = tds.get(5).text();
                    huoBi.setZz(currency5);
                    String currency6 = tds.get(6).text();
                    huoBi.setBegintime(currency6);
                    String currency7 = tds.get(7).text();
                    huoBi.setFbtime(currency7);
                    String currency = tds.text();
                    currencies.add(currency);
                    huobis.add(huoBi);
                }
            } else {
                System.out.println("未找到包含货币信息的表格。"); // 或者执行其他逻辑
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        huobis.forEach(huobi -> System.out.println("222222222222"+huobi));


//        System.out.println("222222"+currencies);
        return huobis;
}}